package zachg.survival;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JLabel;

import com.samskivert.swing.util.SwingUtil;
import com.threerings.parlor.game.data.GameObject;
import com.threerings.parlor.turn.client.TurnDisplay;
import com.threerings.util.Name;

/**
 * This class extends TurnDisplay to include a score indicator next to the icons
 */
@SuppressWarnings("serial")
public class SurvivalTurnDisplay extends TurnDisplay {

	public SurvivalTurnDisplay()
	{
		
	}
	
	public void setLabelAppend(String textToAppend)
	{
		textAppend = textToAppend;
		createList();
	}

	public void setGameobj(SurvivalObject gameobj)
	{
		_gameobj = gameobj;
		createList();
	}
	
    /**
     * Similar to the createList method in TurnDisplay, 
     * with the addition that it can display the score
     */
    protected void createList ()
    {
        removeAll();
        _labels.clear();

        GridBagLayout gridbag = new GridBagLayout();
        setLayout(gridbag);

        GridBagConstraints iconC = new GridBagConstraints();
        GridBagConstraints labelC = new GridBagConstraints();
        iconC.fill = labelC.fill = GridBagConstraints.BOTH;
        labelC.weightx = 1.0;
        labelC.insets.left = 10;
        labelC.gridwidth = GridBagConstraints.REMAINDER;

        Name[] names = _turnObj.getPlayers();
        GameObject gameobj = (GameObject)_turnObj;
        boolean[] winners = gameobj.winners;
        Name holder = _turnObj.getTurnHolder();
        for (int ii=0, jj=0; ii < names.length; ii++, jj++) {
            if (names[ii] == null) continue;

            JLabel iconLabel = new JLabel();
            if (winners == null) {
                if (names[ii].equals(holder)) {
                    iconLabel.setIcon(_turnIcon);
                }
            } else if (gameobj.isDraw()) {
                iconLabel.setText(_drawText);
            } else if (winners[ii]) {
                iconLabel.setText(_winnerText);
            }
            if(textAppend != null){
            	iconLabel.setText(iconLabel.getText() + textAppend);
            }
            //Display game score
            if(_gameobj != null){
            	SurvivalObject.GameCharacter gameCharacter = _gameobj.getGameCharacterFromName(names[ii]);
            	Log.info("Turn Display gameCharacter:"+gameCharacter);
            	iconLabel.setText(iconLabel.getText() + "Score: "+gameCharacter.currentPoints );
            }            
            iconLabel.setForeground(Color.BLACK);
            _labels.put(names[ii], iconLabel);
            add(iconLabel, iconC);

            JLabel label = new JLabel(names[ii].toString());
            if (_playerIcons != null) {
                label.setIcon(_playerIcons[jj]);
            }
            add(label, labelC);
        }

        SwingUtil.refresh(this);
    }
	
    /** Arbitrary text that can be appended next to the icons */
    protected String textAppend;
    
    /** The distributed object, needed to indicate the scores */
    protected SurvivalObject _gameobj;
    
}
